<?php

namespace server;

use GatewayWorker\Lib\Gateway;
use plugin\webman\gateway\Events;


class worker extends Events
{
    //定时器变量管理
    protected static array $timer = [];

    public static function onWorkerStart($worker)
    {

    }

    public static function onWebSocketConnect($client_id, $data)
    {

        if (isset($data["get"]["token"])) {
            $token = $data["get"]["token"];//获取token
            $info = base64_decode($token);//解密
            $infos = json_decode($info, true);
            if (isset($infos["user_id"])) {
                $user_id = $infos["user_id"];
                //检测用户是否连接上了websocket 如果已经有设备连接上了则不允许再次连接
                if (Gateway::isUidOnline($user_id)) {
                    $old_client_id = Gateway::getClientIdByUid($user_id);
                    foreach ($old_client_id as $c_id) {
                        var_dump("用户已经在线--断开连接");
                        //直接删除此客户端连接
                        Gateway::closeClient($c_id);
                    }
                }
                //如果格式正确判断token是否过期24小时
                if ($infos["u_tm"] > time() - 60 * 60 * 24) {
                    Gateway::bindUid($client_id, $user_id);
                    $_SESSION['uid'] = $user_id;
                    var_dump("用户连接上了websocket",$user_id);
                } else {
                    //过期断开
                    var_dump("token过期--断开连接");
                    //断开此客户端连接
                    Gateway::closeClient($client_id);
                }
            } else {
                var_dump("参数错误--断开连接");
                //断开此客户端连接
                Gateway::closeClient($client_id);
            }

        } else {
            var_dump("参数错误2--断开连接");
            //断开此客户端连接
            Gateway::closeClient($client_id);
        }

    }
    public static function onMessage($client_id, $message)
    {
        //接收到消息
        $message = json_decode($message, true);
        if (isset($message["type"])) {
            switch ($message["type"]) {
                //发送消息
                case "send_message":
                    $user_id = $_SESSION['uid'];
                    $data = $message["data"];
                    //好友ID
                    $to_user_id = $data["to_user_id"];
                    //消息内容
                    $text = $data["text"];

                    //发送消息
                    Gateway::sendToUid($to_user_id, json_encode(["type" => "receive_message", "user_id" => $user_id, "text" => $text]));
                    break;
                //发送语音
                case "send_voice":
                    $user_id = $_SESSION['uid'];
                    $data = $message["data"];
                    //好友ID
                    $to_user_id = $data["to_user_id"];
                    //语音URL
                    $voice_url = $data["voice_url"];
                    //发送语音
                    Gateway::sendToUid($to_user_id,json_encode(["type" => "receive_voice", "user_id" => $user_id, "voice_url" => $voice_url]));
                    break;
                case "ping":
                    Gateway::sendToClient($client_id, json_encode(["type" => "pong"]));
                    break;
                default:
                    Gateway::sendToClient($client_id, json_encode(["type" => "unknown"]));
                    break;
            }
        }
    }



    public static function onClose($client_id)
    {

    }



}